﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.EditPages
{
    public partial class ContractLookup : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lstContract.OnNeedDataSource = lstContract_OnNeedDataSource;
            base.Page_Load(sender, e);

            if (!IsPostBack)
            {
                Utilities.LoadDropDownList(ddlCustomer, Customers.GetAllActiveCustomers().DefaultView, Customers.ColumnNames.CustomerName, Customers.ColumnNames.ID);
            }
        }

        public string SearchFilter
        {
            get { return ViewState["SearchFilter"] == null ? "" : ViewState["SearchFilter"].ToString(); }
            set { ViewState["SearchFilter"] = value; }
        }

        protected void btnSearchPhoneNumber_OnClick(object sender, EventArgs e)
        {
            SearchFilter = View_Contracts.ColumnNames.MobileNumber + " like '%" + txtPhoneNumber.Text + "%'";
            lstContract.Refresh();
        }

        protected void btnSearchCustomer_OnClick(object sender, EventArgs e)
        {
            if (ddlCustomer.SelectedValue != "")
            {
                SearchFilter = View_Contracts.ColumnNames.CustomerName + " like '%" + ddlCustomer.SelectedItem.Text + "%'";
                lstContract.Refresh();
            }
        }

        protected void lstContract_OnNeedDataSource(Object sender, EventArgs e)
        {
            View_Contracts prods = new View_Contracts();
            string cols = "*";
            string sort = lstContract.SortExpression + " " + lstContract.SortDirection;
            int totalRow = prods.Search(SearchFilter, cols, sort.Trim(), lstContract.CurrentPageIndex * lstContract.PageSize, lstContract.PageSize);
            lstContract.VirtualItemCount = totalRow;
            lstContract.DataSource = prods.DefaultView;
        }
    }
}